﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using eeenno.ReaderEngine.Adapters;
using eeenno.ReaderEngine.Utilities;

namespace eeenno.SpeeedReaderUI.Forms
{
    public partial class TwitterForm : Form
    {
        private string _message;

        private delegate void _closeCallback();
        private delegate void _enableFormCallBack(bool enable);
        private delegate void _enableWaitCursorCallback(bool enable);

        public TwitterForm(string url, string title)
        {           
            InitializeComponent();
            EnableWaitCursorCallback(true);
            textBox_message.Text = title + " -> " + WebManager.ShortenUrl(url);
            if (textBox_message.Text.Length < 120)
            {
                textBox_message.Text += " (via @SpeeedReader)";
            }
            label_char_count.Text = (140 - textBox_message.Text.Length).ToString();
            EnableWaitCursorCallback(false);
        }

        #region UI callbacks

        private void CloseCallback()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new _closeCallback(CloseCallback));
            }
            else
            {
                this.Close();
            }
        }

        private void EnableFormCallback(bool enable)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new _enableFormCallBack(EnableFormCallback), enable);
            }
            else
            {
                textBox_message.Enabled = enable;
                menuItem_cancel.Enabled = enable;
                menuItem_tweet.Enabled = enable;
            }
        }

        private void EnableWaitCursorCallback(bool enable)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new _enableWaitCursorCallback(EnableWaitCursorCallback),
                    enable);
            }
            else
            {
                if (enable)
                {
                    Cursor.Current = Cursors.WaitCursor;
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                }
            }
        }

        #endregion

        #region twitter logic        

        private void Tweet(Object stateInfo)
        {
            EnableWaitCursorCallback(true);
            EnableFormCallback(false);
            bool result = TwitterEngine.PostTweet(_message, Settings.twitter_username, Settings.twitter_password);
            EnableWaitCursorCallback(false);
            EnableFormCallback(true);
            if (result)
            {
                EnableWaitCursorCallback(false);
                MainForm.DisplayMessageBox("You have tweeted the article successfully.");
                CloseCallback();
            }
            else
            {
                EnableWaitCursorCallback(false);
                MainForm.DisplayMessageBox("Tweet unsuccessful");
            }
        }

        #endregion

        #region UI events

        private void menuItem_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem_tweet_Click(object sender, EventArgs e)
        {
            _message = textBox_message.Text;
            ThreadFactory.DoWork(Tweet);
            //ThreadFactory.GetThread("PostTweet", Tweet).Start();
        }

        private void textBox_message_TextChanged(object sender, EventArgs e)
        {
            label_char_count.Text = (140 - textBox_message.Text.Length).ToString();
        }

        #endregion

    }
}